package com.ruoyi.tester.executor.compile;

import java.util.ArrayList;
import java.util.List;

/**
 * @author SongJun
 * @description TODO:
 * @date 2022/4/27 11:32
 */
public class WordsGenerator {

    //词法分析
    public static List<List<String>> generate(String mass){
        List<List<String>> sentenceList = new ArrayList<>();
        List<String> wordList = new ArrayList<>();
        if(mass != null){
            //escape是否转义
            boolean escape = false;
            //是否处于双引号之中
            boolean inQuotationMark = false;
            //所处括号层级
            int inBracketsLevel = 0;
            StringBuilder sb = new StringBuilder();
            for(int i=0; i<mass.length(); i++){
                char c = mass.charAt(i);
                if(escape){
                    sb.append(c);
                    escape = false;
                    continue;
                } else if(c == '\\'){
                    escape = true;
                    continue;
                }
                if(c == '"'){
                    inQuotationMark = !inQuotationMark;
                }
                if(inQuotationMark){
                    sb.append(c);
                    continue;
                } else if(c == '('){
                    inBracketsLevel ++;
                } else if(c == ')' && inBracketsLevel > 0){
                    inBracketsLevel --;
                }
                if(inBracketsLevel > 0){
                    sb.append(c);
                    continue;
                }
                if (c == '\r' || c == '\n' || c == ' ') {
                    pushWord(wordList, sb);
                    continue;
                } else if (c == ';') {
                    pushWord(wordList, sb);
                    if (pushSentence(sentenceList, wordList)) {
                        wordList = new ArrayList<>();
                    }
                    continue;
                } else if (c == '=') {
                    pushWord(wordList, sb);
                    wordList.add(String.valueOf(c));
                } else {
                    sb.append(c);
                }

            }
            pushWord(wordList, sb);
            pushSentence(sentenceList, wordList);
        }
        return sentenceList;
    }

    private static boolean pushWord(List<String> wordList, StringBuilder sb){
        if(sb.length() > 0){
            wordList.add(sb.toString());
            sb.setLength(0);
            return true;
        }
        return false;
    }

    private static boolean pushSentence(List<List<String>> sentenceList, List<String> sentence){
        if(!sentence.isEmpty()){
            sentenceList.add(sentence);
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        String param =
                "  teacherId=list(0).map(data).map(employeeId);\n" +
                "token=loginToken();\n" +
                "token=token.subString(0,5);\n" +
                "return token teacherId  ";
        List<List<String>> generate = generate(param);
        System.out.println(generate);
    }
}
